/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *
 *  6, April, 2011. Created
 * 10, Feb. 2014. Trivial modifications were made in order to make the code compatibe with Java 1.7 spec
 */

package portfolio.dbHost.dataBases;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import portfolio.dbHost.Host;
import portfolio.dbHost.dataBases.dbTable.OdbcTable;
import portfolio.mainScreen;
//import portfolio.dbHost.dataBases.DBase;

/**
 *
 * @author aca ldero
 */
public class OdbcDBase extends DBase
{
    public OdbcDBase (mainScreen mainFrameParam,
                     Connection connectionParam,
                     String sDBNameParam) throws Exception
    {
        super (mainFrameParam, connectionParam, sDBNameParam);
    }

    public OdbcDBase (Host dbHostParam, String sDBNameParam) throws Exception
    {
        super (dbHostParam, sDBNameParam);
    }
    public OdbcDBase (Host dbHostParam) throws Exception
    {
        super (dbHostParam);
    }
    /**
     *
     * @throws java.lang.Exception
     */
    public void setTablesVector () throws Exception
    {
        DBase.useDB(connection, sDBName);
//        vTables = DBase.showTables(connection, sDBName);
        vTables = showTables(); //connection, sDBName);

        if (vTables == null)
            return;
        iTablesCount = vTables.size();
        if (iTablesCount > 0) {
            dbTables = new OdbcTable [iTablesCount];
            for (int iIdx = 0; iIdx < iTablesCount; iIdx++) {
                if (this.getDBHost().getDBType() == Host.DB_TYPE_ODBC) {
                    dbTables [iIdx] = new OdbcTable (this, vTables.get(iIdx).toString());

                } else {
                    throw new Exception ("Error: Wrong DB type.");
                }
            }
        }
    }
    /*
     * 
     * 
     */
    public Vector showTables ()
    {
        try {
            Connection connection = super.getDBHost().getConnection();
            String sDBName = super.getDBName();
            
            return showTables (connection, sDBName);
        } catch (Exception ex) {
//            Logger.getLogger(OdbcDBase.class.getName()).log(Level.SEVERE, null, ex);
            super.getDBHost().getMainFrame().displayMessage("OdbcDBase 78", "Error at showTables:" + ex.getMessage());
            return null;
        }
    }
    /*
     * 
     * 
     */
    public static Vector showTables (Connection connectionParam,
                                       String sDBNameParam) throws Exception
    {
        if (connectionParam == null)
            return null;

        Vector outVector = new Vector ();

        Statement statement = connectionParam.createStatement ();
        System.out.println ("showTables catalog: " + connectionParam.getCatalog());
//        statement.execute ("USE "+ sDBNameParam + ";");
//Original        ResultSet resultSet = statement.executeQuery ("SHOW TABLES;");
//        ResultSet resultSet = statement.executeQuery ("SELECT table_name FROM INFORMATION_SCHEMA.TABLES" +
//                              " WHERE table_schema = '" + sDBNameParam + "';");
        String sNativeStmt = connectionParam.nativeSQL("SELECT table_name FROM " + "INFORMATION_SCHEMA.TABLES;");
        System.out.println("Native statement: " + sNativeStmt);
        ResultSet resultSet = statement.executeQuery (sNativeStmt); //"SELECT table_name FROM " + "syscalctable;");

        if (!resultSet.next ()) {
            return null;
        }

        do {
            String sTmp = resultSet.getString (1); //.toLowerCase();
//System.out.println (sTmp+"<<<<<<<<");
            if (!outVector.contains(sTmp)) {
                outVector.addElement (sTmp);
            }
        } while (resultSet.next ());

        return (outVector);
    } /* End Method PortfolioMainScr::stocksMenu::showDBTables */
}
